#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
using namespace std;

int main()
{
	pid_t pid = fork();
	if (pid == 0)
	{
		// 子进程
		cout << "i am child process" << endl;
		sleep(5);
	}
	else if (pid > 0)
	{
		// 父进程
		int status = 0;
		waitpid(pid, &status, 0);

		// 获取core_dump
		bool core_dump = WCOREDUMP(status);
		if (core_dump)
		{
			cout << "get core_dump" << endl;
		}
		else
		{
			cout << "not get core_dump" << endl;
		}

		// 获取退出码
		if (WIFEXITED(status))
		{
			int exitcode = WEXITSTATUS(status);
			cout << "exitcode: " << exitcode << endl;
		}
		else
		{
			cout << "not get exitcode" << endl;
		}
	}
	else
	{
		perror("fork error");
		return 1;
	}

	return 0;
}
